package com.dengyuanke.wx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.ShoppingCart;
import com.dengyuanke.service.IShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Api(tags = "购物车相关API")
@RestController
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @ApiOperation(value="添加购物车", notes="传入购物车ShoppingCart对象")
    @PostMapping("/addshoppingcart")
    public JsonResult addShoppingCart(ShoppingCart shoppingCart){
      Boolean result=shoppingCartService.addShoppingCart(shoppingCart);
      if(result){
          return JsonResult.renderSuccess("添加购物车成功");
      }else {
          return JsonResult.renderFail("添加失败");
      }
    }

    /**
     * 更新购物车
     * @param shoppingCart
     * @return
     */
    @ApiOperation(value="更新购物车",notes = "传入购物车实体,必须有id")
    @PostMapping("/updateshoppingcart")
    public JsonResult updateShoppingCart(ShoppingCart shoppingCart){
        Integer total = shoppingCart.getTotal();
        if(total<0){
            return JsonResult.renderFail("购买总数不能小于0");
        }
        Boolean result=shoppingCartService.updateById(shoppingCart);
        if(result){
            return JsonResult.renderSuccess("更新成功");
        }else {
            return JsonResult.renderFail("更新失败");
        }
    }

    /**
     * 通过用户id获取购物车内容
     * @param userId
     * @return
     */
    @ApiOperation("通过用户id获取购物车内容")
    @ApiParam(value="用户id",name="userId",required=true)
    @PostMapping("/getshoppingcart")
    public JsonResult getShoppingCart(String userId){
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectList(new EntityWrapper<ShoppingCart>().eq("creator_id", userId).ge("total", 0));
        return JsonResult.renderSuccess(shoppingCarts);
    }
	
}
